package com.dao;

import java.util.List;

import com.model.Orders;
import com.model.Product;
import com.util.JdbcUtil;

public class OrderDao {
	JdbcUtil jdbc=new JdbcUtil();
	//添加一条订单页信息
	public void add(Orders orders) throws Exception{
		String sql ="INSERT INTO `orders` VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		jdbc.updatePreparedStatement(sql,orders.getOid(),orders.getUid(),orders.getEid(),orders.getTotal(),
				orders.getSubmittime(),orders.getDestinetime(),orders.getOkind(),
				orders.getOstatus(),orders.getUaddress(),orders.getWaddress());
		jdbc.close();
	}
	//订单页进行分页展示
	public List<Orders> queryOrders_show(Orders order,int cp,int pagesize){
		List<Orders> orders = null;
		StringBuffer sql = new StringBuffer("select * from orders where 1=1");
		//添加查询条件
		if(order.getUid()!=null&&order.getUid()!=0){
			sql.append(" and uid = "+order.getUid());
		}

		sql.append(" order by submittime  desc");
		//外层分页语句
		StringBuffer sb = new StringBuffer();
		sb.append(sql);
		sb.append(" limit "+(cp-1)*pagesize+","+(cp)*pagesize);
		System.out.println(sb.toString());
		orders = jdbc.queryPreparedStatement(sb.toString(), Orders.class);
		jdbc.close();
		return orders;
	}
	//查询总条数 根据需求变化,与queryOrders_show搭配使用
	public int count_user(Orders order){
		List<Orders> orders = null;
		StringBuffer sql = new StringBuffer("select * from orders where 1=1");
		//添加查询条件
		if(order.getUid()!=null&&order.getUid()!=0){
			sql.append(" and uid = "+order.getUid());
		}
		List<Orders> temp =  jdbc.queryPreparedStatement(sql.toString(), Orders.class);
		jdbc.close();
		return temp.size();
	}
	//通过oid改变刚下单的订单状态改为已取消
	public void updateOrderStatus(String oid,String ostatus) throws Exception{
		System.out.println(oid);
		String sql = "update orders set ostatus = '"+ostatus+"' where oid = '"+oid+"'";
		System.out.println(sql);
		jdbc.updatePreparedStatement(sql);
	}
}
